package com.propertyportal.manageproperty;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.actionbarsherlock.view.*;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.propertyportal.propertylistfragment.*;

public class ManageProperty extends SherlockFragmentActivity implements PropertyList.OnClickSelectedListener{
	public SlidingMenu menu;
	ActionBar bar;
	PropertyList pl;
	public static int clickposition;
	public static boolean looker;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage_property);

		//Configuration config = getResources().getConfiguration();
		
		bar = getSupportActionBar();
		bar.setTitle("Property Portal");
		bar.setHomeButtonEnabled(true);
		bar.setDisplayHomeAsUpEnabled(true);
		bar.setLogo(R.drawable.property);
		// bar.setBackgroundDrawable(new
		// ColorDrawable(R.drawable.actionbarbackground));

		if (findViewById(R.id.fragment1) != null) {
			looker = true;
			if (savedInstanceState != null) {
				return;
			}
			pl = new PropertyList();
			getSupportFragmentManager().beginTransaction()
					.add(R.id.fragment1, pl).commit();
//			AddPerson ap=new AddPerson();
//			getSupportFragmentManager().beginTransaction().add(R.id.fragment1, ap).commit();
		} else {

			pl = new PropertyList();
			getSupportFragmentManager().beginTransaction()
					.add(R.id.headlines_fragment, pl).commit();
			
			PropertyDetail ma = new PropertyDetail();
			getSupportFragmentManager().beginTransaction()
					.add(R.id.article_fragment, ma).commit();
		}
		menu = new SlidingMenu(this);
		menu.setMode(SlidingMenu.LEFT);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		menu.setMenu(R.layout.slidingmenu);

	}

	public SlidingMenu getSlidingMenu() {
		return menu;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			FragmentManager fm = this.getSupportFragmentManager();
			if(fm.getBackStackEntryCount()!=0){
				fm.popBackStack();
			}else{
				this.menu.toggle();
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onBackPressed() {
			
		if (menu.isMenuShowing()) {
				menu.toggle();
		} else {
			super.onBackPressed();
		}

	}

	@Override
	public void onArticleSelected(View v, int position) {
		// TODO Auto-generated method stub
		PropertyDetail pd=(PropertyDetail)getSupportFragmentManager().findFragmentById(R.id.article_fragment);
		if(pd!=null){
			pd.updateViewShowDetail(position);
			clickposition=position;
		}
		else {
            // If the frag is not available, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            
            Bundle args = new Bundle();
            PropertyDetail pvd=new PropertyDetail();
            args.putInt(PropertyDetail.ARG_POSITION, position);
            pvd.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment1, pvd);
            transaction.addToBackStack(null);
            
            clickposition=position;
            // Commit the transaction
            transaction.commit();
        }
	}

	@Override
	public void onEditSelected(View v, int position) {
		// TODO Auto-generated method stub
		EditProperty pd=(EditProperty)getSupportFragmentManager().findFragmentById(R.id.article_fragment);
		if(pd!=null){
			pd.displayData(position);
			clickposition=position;
		}
		else {
            // If the frag is not available, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            
            Bundle args = new Bundle();
            EditProperty pvd=new EditProperty();
            args.putInt(EditProperty.ARG_POSITION, position);
            pvd.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment1, pvd);
            transaction.addToBackStack(null);
            
            clickposition=position;
            // Commit the transaction
            transaction.commit();
        }
	}

	@Override
	public void onClickDelete(View v, int position) {
		// TODO Auto-generated method stub
		DeleteProperty dl=new DeleteProperty();
		dl.execute(PropertyList.property.get(position).getPropertyId(),0,2);
		
	}

	@Override
	public void onInsertImage(View v, int position) {
		// TODO Auto-generated method stub
		AddImage pd=(AddImage)getSupportFragmentManager().findFragmentById(R.id.article_fragment);
		if(pd!=null){
			pd.displayData(position);
			clickposition=position;
		}
		else {
            // If the frag is not available, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            
            Bundle args = new Bundle();
            AddImage pvd=new AddImage();
            args.putInt(EditProperty.ARG_POSITION, position);
            pvd.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment1, pvd);
            transaction.addToBackStack(null);
            
            clickposition=position;
            // Commit the transaction
            transaction.commit();
        }
	}
	
}
